import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home/index.vue'
import Types from '@/views/Types'
import Video from '@/views/Video'
import Cart from '@/views/Cart'
import My from '@/views/My/index.vue'
import Myindex from '@/views/My/myindex.vue'
import Login from '@/views/My/login.vue'
import Register from '@/views/My/register.vue'
import store from '@/store/index'
import Address from '@/views/Address'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path:'/types',
    component:Types,
  },
  {
    path:'/videos',
    component:Video
  },
  {
    path :'/cart',
    component:Cart,
  },
  {
    path:'/my',
    component:My,
    children:[
      {
        path:'/',
        component:Myindex,
      },
      {
        path:'login',
        component:Login,
      },
      {
        path:'register',
        component:Register,
      },
    ]
  },
  {
    path:'/address',
    component:Address
  }

]

const router = new VueRouter({
  routes
})


//路由守卫
router.afterEach((to,from) =>{
  var active
  if(to.path.startsWith('/my')){
    active =4
  }else{
    active = routes.findIndex(item => item.path === to.path)
  }
  //将这里定义的active赋值给共享数据store
  store.commit('changeActive',active)
})
export default router
